package com.hlm.st_liveness_detector;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import java.util.HashMap;
import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

public class StLivenessDetectorPlugin implements FlutterPlugin, ActivityAware {
  private ActivityPluginBinding binding;
  private MethodChannel.Result result;
  private final PluginRegistry.ActivityResultListener activity_result = (requestCode, resultCode, data) -> {
    if (resultCode==-1){
      Map<String,Object> map=new HashMap<>();
      map.put("code","0");
      map.put("data",data.getByteArrayExtra("byte"));
      map.put("message","success");
      if(result!=null)result.success(map);
    }else{
      if(result!=null)result.success(null);
    }
    return false;
  };
  @RequiresApi(api = Build.VERSION_CODES.M)
  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding plugin_binding) {
    MethodChannel channel = new MethodChannel(plugin_binding.getBinaryMessenger(), "st_liveness_detector");
    channel.setMethodCallHandler((call, result) -> {
      this.result=result;
      switch (call.method) {
        case "startDetect":
          binding.getActivity().startActivityForResult(new Intent(binding.getActivity(), SilentLivenessActivity.class), 0);
          break;
        case "checkPermission":
          if (checkPermission(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            result.success(true);
          } else {
            result.success(false);
          }
          break;
        case "requestPermission":
          binding.getActivity().requestPermissions(new String[]{
                  Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE
          },0);
          result.success(true);
          break;
        default:
          result.notImplemented();
          break;
      }
    });
  }
  protected boolean checkPermission(String... permissions) {
    if (permissions == null || permissions.length < 1) {
      return true;
    }
    if (Build.VERSION.SDK_INT < 23) {
      return true;
    }
    for (String permission : permissions) {
      if (binding.getActivity().checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
        return false;
      }
    }
    return true;
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
  }
  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    this.binding = binding;
    this.binding.addActivityResultListener(activity_result);
  }
  @Override
  public void onDetachedFromActivityForConfigChanges() {
  }
  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
  }
  @Override
  public void onDetachedFromActivity() {
    binding.removeActivityResultListener(activity_result);
  }
}
